package com.sun.showcase.controller.mall;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import com.sun.showcase.biz.mall.service.PlatformConfigService;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Result;
import com.sun.showcase.client.domain.mall.PlatformConfig;
import com.sun.showcase.client.query.mall.PlatformConfigQuery;

@Controller
@RequestMapping(value="/mall/platformConfig") 
public class PlatformConfigController{
	
	@Autowired
	private PlatformConfigService platformConfigService;
	
	private Result result = new Result();
	/**
	 * 跳转到PlatformConfig管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="goPlatformConfig")
	public String goPlatformConfig() {
		PlatformConfig c = platformConfigService.get();
		if(c == null) {
			PlatformConfigQuery q = new PlatformConfigQuery();
			q.setId(1);
			q.setOrderCompleteDay(7);
			q.setOrderReturnDay(7);
			q.setShopCashFee(0D);
			q.setShopSettleDay(30);
			q.setAgentAmount(100D);
			platformConfigService.add(q);
		}
		return "mall/platformConfig";
	}
	/**
	 * 跳转到PlatformConfig新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value="handlePlatformConfig")
	public String handlePlatformConfig(@RequestParam(value="id",required=false) Long id ,ModelMap map) {
		return "mall/platformConfig_rowEdit";
	}

	/**
	 * 跳转到查看desc页面
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="showDesc")
	public PlatformConfig showDesc(PlatformConfigQuery platformConfigQuery) {
		return platformConfigService.get(platformConfigQuery);
	}

	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/datagrid")
	public DataGrid datagrid(PlatformConfigQuery platformConfigQuery) {
		return platformConfigService.datagrid(platformConfigQuery);
	}
	

	/**
	 * 添加一个PlatformConfig
	 */
	@ResponseBody
	@RequestMapping(value="add")
	public Result add(PlatformConfigQuery platformConfigQuery) {
		platformConfigService.add(platformConfigQuery);
		result.setSuccess(true);
		result.setMsg("添加成功！");
		return result;
	}

	/**
	 * 编辑PlatformConfig
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	public Result edit(PlatformConfigQuery platformConfigQuery) {
		platformConfigQuery.setId(1);
		if(platformConfigQuery.getShopSettleDay()<platformConfigQuery.getOrderReturnDay()) {
			result.setSuccess(false);
			result.setMsg("【订单结算周期】必须大于【订单可退货期限】");
			return result;
		}
		platformConfigService.update(platformConfigQuery);
		result.setSuccess(true);
		result.setMsg("编辑成功！");
		return result;
	}
	
	/**
	 * 全部数据PlatformConfig
	 */
	@ResponseBody
	@RequestMapping(value="listAll")
	public Result listAll(PlatformConfigQuery platformConfigQuery) {
		List<PlatformConfigQuery> list = platformConfigService.listAll(platformConfigQuery);
		result.setSuccess(true);
		result.setMsg("成功！");
		result.setData(list);
		return result;
	}
}
